﻿
var _table;
var _oldText = "";
var _oldPageId = 1;
var _oldFindHeadersChecked = false;
var _oldFindContextChecked = false;

function OnLoad() {
    AddCss("/Controls/FindControlStyles.css");
    
    var cacheText = $get("findText").value;
    var cachePageId = 1;

    var footer = $get('findResultPaging');
    if (footer != null && footer != 'undefined') {
        var elems = div.getElementsByTagName('a');
        for (var i = 0; i < elems.length; i++) {
            if (elems[i].getAttribute('class').indexOf('pagingitem_selected') > 0) {
                cachePageId = elems[i].innerText;
                break;
            }            
        }
    }

    if (cacheText != null && cacheText != 'undefined' && cacheText.length > 0) {
        findByText(cacheText, cachePageId);
    }
}

function AddCss(scrName) {
    var tag = document.createElement("link");
    tag.rel = "stylesheet";
    tag.type = "text/css";
    tag.href = scrName;
    document.body.appendChild(tag);
}

function getTable() {
    if (_table == null)
        _table = $get("findResultTable");
    return _table;
}

function setResultVisible(visible) {
    getTable().style.visibility = visible ? "visible" : "hidden";
    return visible;
}

function findByText(text, pageId) {
    var findHeaders = $get('findHeaders').checked;
    var findContext = $get('findContext').checked;

    if (_oldText != null && _oldText.length > 0 && text.length > 0 && text == _oldText && _oldPageId == pageId &&
        findHeaders != _oldFindHeadersChecked && findContext != _oldFindContextChecked) {
            return;
    }

    _oldPageId = pageId;
    _oldText = text;
    _oldFindHeadersChecked = findHeaders;
    _oldFindContextChecked = findContext;

    if (!setResultVisible(text.length > 0)) {
        ClearTable();       
        return;
    }

    SetWaiting();
    Blog.Finder.FindData(text, pageId, findHeaders, findContext, succeededLoading, failedLoading);
}

function succeededLoading(resultData) {
    ClearTable();

    if (resultData != null) {
        FillTable(resultData.Items);
        FillPages(resultData.PagesCount)
    }
}

function failedLoading(error) {
    ClearTable();
    AddTableRow(getTable(), "Stack Trace: " + error.get_stackTrace(), false);
    AddTableRow(getTable(), "Error: " + error.get_message(), false);
    AddTableRow(getTable(), "Status Code: " + error.get_statusCode(), false);
    AddTableRow(getTable(), "Exception Type: " + error.get_exceptionType(), false);
    AddTableRow(getTable(), "Timed Out: " + error.get_timedOut(), false);
}

function SetWaiting() {
    ClearTable();
    AddTableRow(getTable(), "Ожидайте....", true);
}

function ClearTable() {
    var tbl = getTable();
    for (var i = tbl.childNodes.length - 1; i > 0; i--) 
        tbl.removeChild(tbl.childNodes[i]);    
}

function AddTableRow(tbl, text, isHeader) {
    var tr = document.createElement('tr');
    var td = document.createElement(isHeader ? 'th' : 'td');
    tbl.appendChild(tr);
    tr.appendChild(td);
    td.appendChild(document.createTextNode(text));

    return tr;
}

function FillTable(dataCollection) {
    var tbl = getTable();
    AddTableRow(tbl, "Результаты поиска:", true);

    for (var i = 0; i < dataCollection.length; i++) {
        var tr = AddTableRow(tbl, dataCollection[i].ThemeHeader, false);
        tr.setAttribute("onclick", "findItemClick(" + dataCollection[i].ThemeId + ")");
    }
}

function findItemClick(itemId) {
    var url = "/Comments/CommentsTree?";
    url += "id=" + itemId;
    window.location.href = url;
}

function FillPages(pageCount) {
    var tbl = getTable();
    var footer = $get('findResultPaging');
    
    if (footer == null || footer == 'undefined') {
        if (pageCount <= 1)
            return;

        footer = document.createElement('tfoot');
        footer.setAttribute('id', 'findResultPaging');
        tbl.appendChild(footer);
    }
    else {
        if (pageCount <= 1) {            
            tbl.removeChild(footer);
            return;
        }
        
        for (var i = footer.childNodes.length - 1; i > 0; i--)
            footer.removeChild(footer.childNodes[i]);
    }

    var td = document.createElement('td');
    footer.appendChild(td);

    td.appendChild(document.createTextNode("Страницы: "));

    for (i = 1; i <= pageCount; i++) {
        var aLink = document.createElement('a');
        aLink.setAttribute('href', 'javascript:findByText("' + _oldText + '",' + i + ')');
        var classStr = 'more pagingitem';
        if (_oldPageId == i)
            classStr += ' pagingitem_selected';
        aLink.setAttribute('class', classStr);
        aLink.innerText = i;
        td.appendChild(aLink);
    }
}
